package com.youlai.boot.system.service.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.converter.ArticleConverter;
import com.youlai.boot.system.mapper.ArticleMapper;
import com.youlai.boot.system.model.entity.Article;
import com.youlai.boot.system.model.form.ArticleForm;
import com.youlai.boot.system.model.query.ArticlePageQuery;
import com.youlai.boot.system.model.vo.ArticleVO;
import com.youlai.boot.system.model.vo.ArticlePageVO;
import com.youlai.boot.system.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.youlai.boot.system.model.bo.ArticleBO;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

/**
 * 部门 业务实现类
 *
 * @author Ray
 * @since 2021/08/22
 */
@Service
@RequiredArgsConstructor
//public class ArticleTypeServiceImpl extends ServiceImpl<ArticleTypeMapper, ArticleType> implements ArticleTypeService {
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    private final ArticleConverter ArticleConverter;
    private final PasswordEncoder passwordEncoder;
    /**
     * 角色分页列表
     *
     * @param queryParams 角色查询参数
     * @return {@link Page <  ArticlePageVO  >} – 角色分页列表
     */
    @Override
    public IPage<ArticlePageVO> listPagedArticles(ArticlePageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<ArticleBO> page = new Page<>(pageNum, pageSize);
        // 查询数据
        Page<ArticleBO> ArticlePage = this.baseMapper.listPagedArticles(page, queryParams);
        // 实体转换
        return ArticleConverter.toPageVo(ArticlePage);
    }

    /**
     * 热门数据
     * @return {@link Page <  ArticlePageVO  >} – 热门数据
     */
    @Override
    public List<ArticlePageVO> getHotArticle(ArticlePageQuery queryParams) {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getHotArticle(queryParams);
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }

    /**
     * 热门数据
     * @return {@link Page <  ArticlePageVO  >} – 热门数据
     */
    @Override
    public List<ArticlePageVO> getIndexHotArticle(int parent_id) {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getIndexHotArticle(parent_id);
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }

    /**
     * 角色分页列表
     *
     * @param queryParams 角色查询参数
     * @return {@link Page <  ArticlePageVO  >} – 角色分页列表
     */
    @Override
    public IPage<ArticlePageVO> fllistPagedArticles(ArticlePageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<ArticleBO> page = new Page<>(pageNum, pageSize);
        // 查询数据
        Page<ArticleBO> ArticlePage = this.baseMapper.fllistPagedArticles(page, queryParams);
        // 实体转换
        return ArticleConverter.toPageVo(ArticlePage);
    }


    /**
     * 角色分页列表
     *
     * @param queryParams 角色查询参数
     * @return {@link Page <  ArticlePageVO  >} – 角色分页列表
     */
    @Override
    public IPage<ArticlePageVO> keyWordsPagedArticles(ArticlePageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<ArticleBO> page = new Page<>(pageNum, pageSize);
        // 查询数据
        Page<ArticleBO> ArticlePage = this.baseMapper.keyWordsPagedArticles(page, queryParams);
        // 实体转换
        return ArticleConverter.toPageVo(ArticlePage);
    }




    /**
     * 角色分页列表
     * @return {@link Page <  ArticlePageVO  >} – 角色分页列表
     */
    @Override
    public List<ArticlePageVO> getRelateArticle(Long articleId, int flid) {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getRelateArticle(articleId, flid);
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }





    /**
     * 推荐数据
     * @return {@link Page <  ArticlePageVO  >} – 推荐数据
     */
    @Override
    public List<ArticlePageVO> getTjArticle() {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getTjArticle();
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }

    /**
     * 推荐数据
     * @return {@link Page <  ArticlePageVO  >} – 推荐数据
     */
    @Override
    public List<ArticleBO> getTjArticlev1() {
        // 查询数据
        List<ArticleBO> Article = this.baseMapper.getTjArticlev1();
        return Article;
    }

    /**
     * 分类热门数据
     * @return {@link Page <  ArticlePageVO  >} – 分类热门数据
     */
    @Override
    public List<ArticlePageVO> getFlHotArticle(int flid) {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getFlHotArticle(flid);
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }

    /**
     * 分类推荐数据
     * @return {@link Page <  ArticlePageVO  >} – 分类推荐数据
     */
    @Override
    public List<ArticlePageVO> getFlTjArticle(int flid) {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getFlTjArticle(flid);
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }


    /**
     * 分类最新数据
     * @return {@link Page <  ArticlePageVO  >} – 分类最新数据
     */
    @Override
    public List<ArticlePageVO> getFlZxArticle(int flid) {
        // 查询数据
        List<ArticleBO> ArticlePage = this.baseMapper.getFlZxArticle(flid);
        // 实体转换
        return ArticleConverter.toListVo(ArticlePage);
    }




//    /**
//     * 分类热门数据
//     * @return {@link Page <  ArticlePageVO  >} – 分类热门数据
//     */
//    @Override
//    public List<ArticlePageVO> geFlHotArticle() {
//        // 查询数据
//        List<ArticleBO> ArticlePage = this.baseMapper.geFlHotArticle();
//        // 实体转换
//        return ArticleConverter.toListVo(ArticlePage);
//    }


    /**
     * 新增文章
     *
     * @param articleForm 用户表单对象
     * @return
     */
    @Override
    public boolean saveArticle(ArticleForm articleForm) {
        // 实体转换 form->entity
        Article entity = ArticleConverter.toEntity(articleForm);
        boolean result = this.save(entity);
        return result;
    }

    /**
     * 更新用户
     *
     * @param articleId   用户ID
     * @param articleForm 用户表单对象
     * @return
     */
    @Override
    @Transactional
    public boolean updateArticle(Long articleId, ArticleForm articleForm) {
        Article entity = ArticleConverter.toEntity(articleForm);
        boolean result = this.updateById(entity);
        return result;
    }


    /**
     * 获取部门表单
     *
     * @param articleId 部门ID
     * @return 部门表单对象
     */
    @Override
    public ArticleVO getArticleForm(Long articleId) {
        ArticleBO ArticleForm = this.baseMapper.getArticleForm(articleId);
        return ArticleConverter.toVo(ArticleForm);
    }

    /**
     * 获取文章上一页数据
     *
     * @param articleId 部门ID
     * @param flid
     * @return 文章上一页表单对象
     */
    @Override
    public ArticleVO getArticlePrevData(Long articleId, int flid) {
        ArticleBO ArticleForm = this.baseMapper.getArticlePrevData(articleId,flid);
        return ArticleConverter.toVo(ArticleForm);
    }


    /**
     * 获取文章上一页数据
     *
     * @param articleId 部门ID
     * @param flid
     * @return 文章上一页表单对象
     */
    @Override
    public ArticleVO getArticleNextData(Long articleId, int flid) {
        ArticleBO ArticleForm = this.baseMapper.getArticleNextData(articleId,flid);
        return ArticleConverter.toVo(ArticleForm);
    }


    /**
     * 删除用户
     * @param idsStr 用户ID，多个以英文逗号(,)分割
     * @return true|false
     */
    @Override
    public boolean deleteArticles(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean viewArticle(Long articleId) {
        boolean view_result = this.baseMapper.viewArticle(articleId);
        return view_result;
    }


}
